﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;

using ShootSearch.Core;
using ShootSearch.Util;
using System.ComponentModel;

namespace ShootSearch.Indexers
{
    [Serializable]
    [TypeConverter(typeof(Util.ExpandableConverter<DBIndexerConfig>))]
    public class DBIndexerConfig:SpiderManConfig//, ICloneable
    {
        public AssemblyConfig Connection { get; set; }
        public string ConnectionString { get; set; }
        public string Table { get; set; }
        public string TableCreation { get; set; }
        public string TableCheck { get; set; }
        public string UniqueNameSpliter { get; set; }
        public string Insert { get; set; }
        public List<DBIndexerAttribute> InterestedAttributes { get; set; }


        public DBIndexerConfig()
        {
            Connection = new AssemblyConfig(typeof(SqlConnection));
            InterestedAttributes = new List<DBIndexerAttribute>();
            UniqueNameSpliter= "[{0}]";
            Insert = "INSERT INTO ({0}) values({1})";
           
        }

        [Serializable]
        [TypeConverter(typeof(Util.ExpandableConverter<DBIndexerAttribute>))]
        public class DBIndexerAttribute : IndexerAttribute
        {
            public DbType DbType { get; set; }


            public DBIndexerAttribute()
            {
 
            }

            public DBIndexerAttribute(string name, DbType dbType, bool allowNull)
                : base(name)
            {
                this.DbType = DbType;
                this.AllowNull = allowNull;
            }

            public DBIndexerAttribute(string name, DbType dbType, bool allowNull, string format)
                : base(name)
            {
                this.DbType = DbType;
                this.AllowNull = allowNull;
                this.Format = format;
            }

            public object GetDbTypeValue(string value)
            {
                if (value == null) return null;
                object objValue = null;
                try
                {
                    #region DbType
                    switch (DbType)
                    {
                        case DbType.Boolean:
                            objValue = Convert.ToBoolean(value);
                            break;
                        case DbType.Byte:
                            objValue = Convert.ToByte(value);
                            break;
                        case DbType.Decimal:
                            objValue = Convert.ToDecimal(value);
                            break;
                        case DbType.Double:
                            objValue = Convert.ToDouble(value);
                            break;
                        case DbType.Int16:
                            objValue = Convert.ToInt16(value);
                            break;
                        case DbType.Int32:
                            objValue = Convert.ToInt32(value);
                            break;
                        case DbType.Int64:
                            objValue = Convert.ToInt64(value);
                            break;
                        case DbType.Date:
                        case DbType.DateTime:
                        case DbType.DateTime2:
                        case DbType.DateTimeOffset:
                        case DbType.Time:
                            objValue = Convert.ToDateTime(value);
                            break;
                        case DbType.Guid:
                            objValue = new Guid(value);
                            break;
                        case DbType.SByte:
                            objValue = Convert.ToSByte(value);
                            break;
                        case DbType.UInt16:
                            objValue = Convert.ToUInt16(value);
                            break;
                        case DbType.UInt32:
                            objValue = Convert.ToUInt32(value);
                            break;
                        case DbType.UInt64:
                            objValue = Convert.ToUInt64(value);
                            break;
                        case DbType.Single:
                            objValue = Convert.ToSingle(value);
                            break;
                        default:
                            objValue = value;
                            break;
                    } 
                    #endregion
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                }
                

                return objValue;
            }
           

           
        }
    }

    


}
